/*
 * $Id: LbsSimulatorImpl.java,v 1.4 2009/07/24 18:27:07 oliver Exp $
 *
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 *
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

package com.ge.healthcare.autosc.processor.test;

import java.util.Date;

import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.rmi.PortableRemoteObject;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.dao.ITaskCategoryInfoDAO;
import com.ge.healthcare.autosc.common.database.dao.ITaskTypeDAO;
import com.ge.healthcare.autosc.common.database.pojo.ActTask;
import com.ge.healthcare.autosc.common.database.pojo.CfgTaskCategoryInfo;
import com.ge.healthcare.autosc.common.database.pojo.CfgTaskType;
import com.ge.healthcare.autosc.common.util.ServiceLocator;

public class LbsSimulatorImpl implements LbsSimulator {
	private ITaskCategoryInfoDAO taskCategoryDAO = null;
	private ITaskTypeDAO taskTypeDAO = null;
	private ServiceLocator serviceLocator = null;

	public void sendTaskInfo(ActTask actTask) throws JMSException {
		System.out.println("LBS Sent at " + (new Date()).toString());
		CfgTaskType taskType = taskTypeDAO.getByName(actTask.getTaskType());
		CfgTaskCategoryInfo taskCategoryInfo = taskCategoryDAO
				.getByTaskCategoryId(taskType.getTaskCategoryId());
		this.sendToCategoryQueue(actTask, taskCategoryInfo);
	}

	/**
	 * Sends the task info object to category queue
	 * 
	 * @param taskInfo
	 *            - Task info object
	 * @param rtvdPrcToTask
	 *            - Process info for the given task
	 * @throws JMSException
	 *             - Failed to send the message
	 */
	private void sendToCategoryQueue(ActTask taskInfo,
			CfgTaskCategoryInfo taskCategoryInfo) throws JMSException {
		System.out.println("******" + taskCategoryInfo);
		System.out.println("******" + taskCategoryInfo.getQueueName());
		System.out.println("******" + taskCategoryInfo.getTaskCategory());
		String methodName = "sendObjectToProcessor";
		QueueConnectionFactory queueConnFactory = (QueueConnectionFactory) PortableRemoteObject
				.narrow(serviceLocator.getService("ConnectionFactory"),
						QueueConnectionFactory.class);
		Queue queue = (Queue) PortableRemoteObject.narrow(serviceLocator
				.getService(taskCategoryInfo.getQueueName()), Queue.class);

		QueueConnection queueConnection = null;
		QueueSession queueSession = null;
		QueueSender queueSender = null;
		try {
			queueConnection = queueConnFactory.createQueueConnection();
			queueSession = queueConnection.createQueueSession(false,
					Session.AUTO_ACKNOWLEDGE);
			queueSender = queueSession.createSender(queue);
			ObjectMessage objMessage = queueSession
					.createObjectMessage(taskInfo);
			queueSender.send(objMessage);
			ASCLogger.debug(this.getClass(), methodName,
					"Message sent successfully");
		} finally {
			this.cleanUp(queueSender, queueSession, queueConnection);
		}
	}

	/**
	 * Release the queue connections
	 */
	private void cleanUp(QueueSender queueSender, QueueSession queueSession,
			QueueConnection queueConnection) {
		try {
			if (!ASCUtil.isNullOrEmptyObject(queueSender)) {
				queueSender.close();
				queueSender = null;
			}
		} catch (Throwable e) {
			queueSender = null;
		}
		try {
			if (!ASCUtil.isNullOrEmptyObject(queueSession)) {
				queueSession.close();
				queueSession = null;
			}
		} catch (Throwable e) {
			queueSession = null;
		}
		try {
			if (!ASCUtil.isNullOrEmptyObject(queueConnection)) {
				queueConnection.close();
				queueConnection = null;
			}
		} catch (Throwable e) {
			queueConnection = null;
		}
	}

	/**
	 * @param taskCategoryDAO the taskCategoryDAO to set
	 */
	public void setTaskCategoryDAO(ITaskCategoryInfoDAO taskCategoryDAO) {
		this.taskCategoryDAO = taskCategoryDAO;
	}

	/**
	 * @param taskTypeDAO the taskTypeDAO to set
	 */
	public void setTaskTypeDAO(ITaskTypeDAO taskTypeDAO) {
		this.taskTypeDAO = taskTypeDAO;
	}

	/**
	 * @param serviceLocator the serviceLocator to set
	 */
	public void setServiceLocator(ServiceLocator serviceLocator) {
		this.serviceLocator = serviceLocator;
	}
}
